package com.quanxiaoha.weblog.admin.controller;

import com.quanxiaoha.weblog.admin.model.vo.file.QueryFileExistsReqVO;
import com.quanxiaoha.weblog.admin.service.AdminFileService;
import com.quanxiaoha.weblog.common.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author: 犬小哈
 * @url: www.quanxiaoha.com
 * @date: 2023-04-19 16:06
 * @description: TODO
 **/
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminFileController {

    @Autowired
    private AdminFileService fileService;

    @PostMapping("/file/upload")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response<?> uploadFile(@RequestParam MultipartFile file, @RequestParam String prefix) {
        return fileService.uploadFile(file, prefix, false);
    }

    @PostMapping("/file/upload/saveFilename")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response<?> uploadFileSaveFilename(MultipartFile file, @RequestParam String prefix) {
        return fileService.uploadFile(file, prefix, true);
    }

    @PostMapping("/file/exists")
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    public Response<List<Boolean>> queryFileExists(@RequestBody QueryFileExistsReqVO req) {
        return fileService.queryFileExists(req);
    }

}
